package com.retail.shop.controller;

import cn.hutool.core.util.ObjectUtil;
import com.retail.core.common.AbstractCommonController;
import com.retail.core.entity.ParamReq;

import com.retail.core.entity.result.PageResult;
import com.retail.core.entity.result.Result;
import com.retail.global.security.LoginUser;
import com.retail.global.security.LoginUserUtil;
import com.retail.global.annon.TraceLog;
import com.retail.shop.entity.request.ShopCardLogRequest;
import com.retail.shop.entity.vo.ShopCardLogVo;
import com.retail.shop.service.ShopCardLogService;
import com.retail.system.entity.request.AdminRequest;
import com.retail.system.enums.UserTypeEnums;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 开卡记录 API
* @author Administrator
*/
@Api(tags = "开卡记录 API")
@RestController
@RequestMapping("/shopCardLog")
public class ShopCardLogController extends AbstractCommonController<ShopCardLogService, ShopCardLogRequest, ShopCardLogVo> {

    @Autowired
    private ShopCardLogService shopCardLogService;

    @TraceLog()
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(shopCardLogService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("保存开卡记录")
    public Result<Integer> save(@RequestBody ShopCardLogRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.save(shopCardLogService, request);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新开卡记录")
    public Result<Integer> update(@RequestBody ShopCardLogRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.update(shopCardLogService, request);
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("开卡记录-查询")
    public Result<List<ShopCardLogVo>>listPage(@RequestBody ParamReq req) {
        return super.listPage(shopCardLogService, req);
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("开卡记录-统计")
    public Result<Integer> countPage(@RequestBody ParamReq req) {
        return super.countPage(shopCardLogService, req);
    }

    @TraceLog()
    @PostMapping("page")
    @ApiOperation("开卡记录-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<ShopCardLogRequest> req) {
        ShopCardLogRequest adminRequest = req.getEntity();
        Long adminId = getLoginUserId();
        LoginUser loginUser = LoginUserUtil.getPrincipal();
        adminRequest.setUserType(loginUser.getUserType());
        adminRequest.setSearchOwnerUserId(adminId);
        req.setEntity(adminRequest);


        return super.page(shopCardLogService, req);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除开卡记录")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return super.delete(shopCardLogService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除开卡记录")
    @DeleteMapping("{ids}")
    public Result<Integer> batchDelete(List<Long> ids) {
        return super.batchDelete(shopCardLogService, ids);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }

}
